#!/bin/bash
#2017-09-23 1348
user="`who am i | awk -F" " '{print $1}'`"
newdir="`pwd`/../../bin"
#开机启动脚本名称不要和服务名称一样，因为会执行killall，开机启动脚本会被干掉
#autostart_server_name=autoStart-${server_name}

userpath="/home/${user}"
if [ "${user}" = "root" ]; then
	userpath="/${user}"
fi

function f_autostart(){
	if [ ! -f ${src_file_dir} ]; then
		echo "Error:not find ${src_file_dir} stop install"
		exit
	fi
	cp -f ${src_file_dir} ${obj_file_dir}		#创建新启动脚本
	chown -R ${user}:${user} ${obj_file_dir}
	chmod -R 755 ${obj_file_dir}
	sed -i "s:#*cd.*$:cd ${newdir}:g"  ${obj_file_dir}		#修改启动脚本路径
	sed -i "s:./Phoenix.*$:${newcmd}:g" ${obj_file_dir}		#修改启动脚本启动参数

	if [ ! -d ${packet_path} ]; then
		mkdir ${packet_path}
		chown -R ${user}:${user} ${packet_path}
		chmod -R 700 ${packet_path}
	else 
		chown -R ${user}:${user} ${packet_path}
		chmod -R 700 ${packet_path}		
	fi

#修改源桌面自启动文件参数
	sed -i "s:Exec=.*$:Exec=${obj_file_dir}:g" autostart/${desktopfile}
	sed -i "s:Name\[zh\_CN\]=.*$:Name\[zh\_CN\]=${title_name_cn}:g" autostart/${desktopfile}
	sed -i "s:Name=.*$:Name=${title_name}:g" autostart/${desktopfile}
	sed -i "s:Comment\[zh\_CN\]=.*$:Comment\[zh\_CN\]=${title_name_cn}:g" autostart/${desktopfile}
	sed -i "s:Comment=.*$:Comment=${title_name}:g" autostart/${desktopfile}

#复制源桌面自启动文件到目标路径
	if [ -f ${packet_path}/${packet_desktop} ]; then
		rm -rf ${packet_path}/${packet_desktop}
		cp -f autostart/${desktopfile} ${packet_path}/${packet_desktop}
	else
		cp -f autostart/${desktopfile} ${packet_path}/${packet_desktop}
	fi
	chown ${user}:${user} ${packet_path}/${packet_desktop}
	chmod 664 ${packet_path}/${packet_desktop}

	#echo "Starting ${title_name_cn} ..."
	#bash ${packet_path}
	echo "ok!"
}

function f_delautostart(){

	if [ -f ${packet_path}/${packet_desktop} ]; then
		rm -rf ${packet_path}/${packet_desktop}
	else
		echo "${packet_path}/${packet_desktop} is del"
	fi

}

case "$1" in
	autostart)
		src_file_dir="${newdir}/Phoenix.sh"
		obj_file_dir="${newdir}/.Phoenix_autostart.sh"
		newcmd="./Phoenix"
		desktopfile="Phoenix.desktop"
		title_name=Phoenix.desktop
		title_name_cn=Phoenix.desktop
		packet_path="${userpath}/.config/autostart"
		packet_desktop="Phoenix.desktop"
		f_autostart
	;;
	autoLogin)
		src_file_dir="${newdir}/Phoenix.sh"
		obj_file_dir="${newdir}/.Phoenix_autostart.sh"
		newcmd="./Phoenix autoLoginUser,1"
		desktopfile="Phoenix.desktop"
		title_name=Phoenix.desktop
		title_name_cn=Phoenix.desktop
		packet_path="${userpath}/.config/autostart"
		packet_desktop="Phoenix.desktop"
		f_autostart
	;;
	autoJKServer)
		src_file_dir="${newdir}/PSM70Server.sh"
		obj_file_dir="${newdir}/.PSM70Server_autostart.sh"
		newcmd="./Phoenix"
		desktopfile="PSM70Server.desktop"
		title_name=PSM70Server.desktop
		title_name_cn=PSM70Server.desktop
		packet_path="${userpath}/.config/autostart"
		packet_desktop="PSM70Server.desktop"
		f_autostart
	;;
	delautostart)
		packet_path="${userpath}/.config/autostart"
		packet_desktop="Phoenix.desktop"		
		f_delautostart
	;;
	delJKServer)
		packet_path="${userpath}/.config/autostart"
		packet_desktop="PSM70Server.desktop"		
		f_delautostart
	;;
	*)
		echo "Client:Please use autostart or autoLogin as first argument"
		echo "JKServer:Please use autoJKServer as first argument"
		echo "del:Please use delautostart or delJKServer as first argument"
	;;
esac
